import modules.mysql_packages as st

# cookie获取
def get_cookie_dict(connection,name:str):
    # 将数据库中的cookie列表聚合成字典，并删除重复项
    sql = f"SELECT * FROM `{name}cookie`"
    cookie_list = [[row["key"], row["key_value"]] for row in st.sql_select(connection, sql)]
    unique_cookie_list = []
    for row in cookie_list:
        if row not in unique_cookie_list:
            unique_cookie_list.append(row)
    cookie_dict = {}
    for row in unique_cookie_list:
        cookie_dict[row[0]] = row[1]
    return cookie_dict

# 爆单请求体设置
def get_bd_body(date_str,page,id,name):
    bd_body = {
        "extra": {
            "scene": "overview_table_data_reporting",
            "patch_control_param": "1"
        },
        "common_req": {
            "st": date_str,
            "et": date_str,
            "page": page,
            "page_size": 100,
            "metrics": ["stat_cost", "cpc", "cpm", "show_cnt", "click_cnt", "ctr", "time_attr_convert_cnt",
                        "time_attr_conversion_cost", "time_attr_conversion_rate", "convert_cnt", "conversion_cost",
                        "conversion_rate", "show_uv", "account_type", "skan_convert_cnt", "skan_conversion_cost",
                        "skan_conversion_rate", "time_attr_onsite_shopping_roas", "currency", "currency_precision",
                        "is_diff_currency"],
            "dimensions": [
                "advertiser_id",
                "advertiser_name",
                "adv_timezone"
            ],
            "sort_stat": "stat_cost",
            "sort_order": 1
        },
        "adv_filter": {
            "multilevel_bc": {
                "org_id": id,
                "org_name": name,
                "currency": "USD",
                "timezone": "America/New_York"
            }
        },
        "operation": 1,
        "is_chart": 0
    }
    return bd_body
# 奇迹岛请求体设置
# def get_qjd_body(date_str,page,id,name):
#     qjd_body = {
#         "extra": {
#             "scene": "overview_table_data_reporting",
#             "patch_control_param": "1"
#         },
#         "common_req": {
#             "st": date_str,
#             "et": date_str,
#             "page": page,
#             "page_size": 100,
#             "metrics": [
#                 "stat_cost",
#                 "cpc",
#                 "cpm",
#                 "show_cnt",
#                 "click_cnt",
#                 "ctr",
#                 "time_attr_convert_cnt",
#                 "time_attr_conversion_cost",
#                 "time_attr_conversion_rate",
#                 "convert_cnt",
#                 "conversion_cost",
#                 "conversion_rate",
#                 "show_uv",
#                 "account_type",
#                 "skan_convert_cnt",
#                 "skan_conversion_cost",
#                 "skan_conversion_rate",
#                 "company_name",
#                 "adv_contacter",
#                 "currency",
#                 "time_attr_onsite_shopping_roas",
#                 "currency_precision",
#                 "is_diff_currency"
#             ],
#             "dimensions": [
#                 "advertiser_id",
#                 "advertiser_name",
#                 "adv_timezone"
#             ],
#             "sort_stat": "stat_cost",
#             "sort_order": 1
#         },
#         "adv_filter": {
#             "multilevel_bc": {
#                 "org_id": id,
#                 "org_name": name,
#                 "currency": "ZERO",
#                 "timezone": "America/Los_Angeles"
#             }
#         },
#         "operation": 1,
#         "is_chart": 0
#     }
#     return qjd_body
